//跳转到线路信息页面
var goLineInfo;
//跳转到车辆信息页面
var goCarInfo;
require(['grid', 'topBar','layout', 'date', 'select2'], function () {
	var $grid = $("#grid"),trajectoryReplayDialogId;
	
	//初始化顶部搜索栏
	function topBarInit() {
        $("#top").topBar({
            left: [
                {
                    type: 'filter-more',
                    dateType:'custom',
                    search: function () {
                        search();
                    },
                    item: [
                        {
                            text: '时间快捷',
                            name1: 'beginTime',
                            name2: 'endTime',
                            type: 'dateLine'
                        },
                        {
                            text: '两站间隔时长',
                            name: 'stopTime',
                            width: '160',
                            type: 'select',
                            data: [
                                {text: '30', value: '30'},
                                {text: '60', value: '60'},
                                {text: '90', value: '90'},
                                {text: '120', value: '120'}
                            ]
                        }
                    ]
                }
            ],
            right: [
                {
                    text: '导出',
                    type: 'export-btn',
                    authority: '',
                    item: [
                        {
                            text: 'Excel导出',
                            click: function () {
                                exportFile('Excel');
                            }
                        },
                        {
                            text: 'PDF导出',
                            click: function () {
                                exportFile('Pdf');
                            }
                        }
                    ]
                }
            ]
        });
    }

    function gridInit() {
        $grid.grid({
            url: "/noReasonToStop/searchNoReasonToStopPage.htm",
            colModel: [
                {name: "id",label: 'id',key: true, hidden: true},
                {name: "codeClDsc",label: 'codeClDsc', hidden: true},
                {name: "codeDsc",label: 'codeDsc',hidden: true},
                {name: "drvName",label: '驾驶员',align: "left",hidden: true},
                {name: "carId", label: "carId", hidden: true},
                {name: "lineId", label: "lineId", hidden: true},
                {name: "licPltNo",label: '车牌号',align: "left" ,width: "80",formatter: function (cellvalue,options,rowObject) {
                	return "<b><a href='javascript:void(0);' onclick='goCarInfo(\"" + rowObject.carId + "\")' class='text-link c-blue'>" + cellvalue + "</a></b>";
                }},
                {name: "carInCd", label: "车辆编号", width: 80},
                {name: "lineName",label: '运营线路',align: "left" ,width: "80",formatter: 'customCurrency',formatoptions:{
	                redbtn:true,
	                css:'text-link c-blue',
	                clickFn:function(rowObject,cellvalue){
	                	goLineInfo(rowObject.lineId);
	                }
	            }},
                {name: "organName",label: '所属机构',align: "left" ,width: "80"},
                {name: "upDn",label: '方向',align: "left" ,width: "80"},
                {name: "leaveStnName",label: '离站站点',align: "left"},
                {name: "leaveTime",label: '离站时间',align: "center"},
                {name: "arriveStnName",label: '进站站点',align: "left"},
                {name: "arriveTime",label: '进站时间',align: "center"},
                {name: "intervalMinute",label: '间隔时长',align: "center" ,width: "80"},
                {
                    name: "track", label: "轨迹回放", width: "80", align: "center",
                    formatter: function (cellvalue, options, rowObject) {
                        var spedBeginTime = rowObject.leaveTime,
                            spedEndTime = rowObject.arriveTime,
                            carId = rowObject.carId,
                            corpId = "5eb7f5e696af489da769ea6811e38dfe"
                                //rowObject.corpId,
                            lineId = rowObject.lineId,
                            name = rowObject.organName,
                            province = rowObject.codeClDsc,
                            city = rowObject.codeDsc;
                        var content = '<a href ="javascript:;" class="dia_gj" onclick="showSpedTraj(\'' + carId + '\',\'' + spedBeginTime + '\',\'' + spedEndTime + '\',\'' + corpId + '\',\'' + lineId + '\',\'' + name + '\',\'' + province + '\',\'' + city + '\')">'
                            + "<img src = \'" + $.getProjectName() + "/content/images/track_ico.png'\' alt =''/>"
                            + '</a>';
                        return content;
                    }
                }
            ],
            multiselect: false
        });
    }


    function exportFile(suffix) {
        if ($('#grid').getGridParam("reccount") > 0){
            var url = "/noReasonToStop/exportNoReasonToStop"+suffix+".htm";
            var type = commonFun.getLeftTreeCurrentTreeType();
            var orgIds = commonFun.getLeftTreeCheckedNodes(type);
            if(!orgIds) {
                $.messager.alert("提示", "请选择查询车辆/机构/驾驶员。", "info");
            }else {
                var $postData = $("#top").parent().getValues();
                $postData.type = type;
                $postData.orgIds = orgIds;
                $.exportFile(url, $postData);
            };
        }else{
            $.messager.alert("提示", "请先查询数据。", "info");
        }
        ;
    }

    //获取左侧选择的节点信息
    function getPostData() {
        var rcx = commonFun.getTreeCheckNodeToArgs();
        var type = commonFun.getLeftTreeCurrentTreeType();
        var orgIds = commonFun.getLeftTreeCheckedNodes(type);
        if (rcx) {
            postData = $.extend(rcx, $("#top").parent().getValues());
            postData.type = type;
            postData.orgIds = orgIds;
        }
        return rcx;
    }

    //查询grid
    search = function(){
        if (getPostData()) {
            $grid.grid('setGridParam', {
                datatype: "JSON",
                page: 1,
                postData: postData
            }).trigger("reloadGrid");
        }
    };

    /**
     * 打开轨迹回放弹窗
     */
    showSpedTraj = function (carId, beginTime, endTime, corpId, lineId, name, province, city) {
        console.info(carId, beginTime, endTime, corpId, lineId, name, province, city)
        var data = new Array();  // 在这里构造车辆数据的数组
        var obj = new Object();  //车辆数据
        obj.carId = carId;
        obj.beginTime = beginTime;
        obj.endTime = endTime;
        data.push(obj);
        if (!trajectoryReplayDialogId) {
            trajectoryReplayDialogId = $('<div>').dialog({
                title: '轨迹回放',
                url: './common/carAnaCarTrack.htm',
                height: $(window.parent).height() * 0.8,
                width: $(window.parent).width() * 0.85
            });
        }
        trajectoryReplayDialogId.dialog('open');
        trajectoryReplayDialogId.dialog('exec', 'loadData', carId, beginTime, endTime, name, province, city);
    };
    
    
  //跳转到车辆信息页面
    goCarInfo = function(carId){
    	var data = {};
    	data.carId = carId;
    	$.openTab('6bad880fef164ec6adaea7b18420eb6e','showCarInfo',[data]);
    }
    //跳转到线路信息页面
    goLineInfo = function(lineId){
    	var data = {};
    	data.lineId = lineId;
    	$.openTab('4db17f8a322b4749a61eb2f8a12023ae','showLineInfo',[data]);
    }

    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();

    $("select[name='stopTime']").select2('val',60)
});